<?php
function BBCodeFormat($str, $removeTags = FALSE){
$str = $removeTags ? strip_tags($str) : htmlentities($str);
$str = nl2br($str);
$pattern = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[url\=(.*?)\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
'/\[mail\](.*?)\[\/mail\]/is',
'/\[font\=(.*?)\](.*?)\[\/font\]/is',
'/\[size\=(.*?)\](.*?)\[\/size\]/is',
'/\[color\=(.*?)\](.*?)\[\/color\]/is',
);
$replace = array(
'<strong></strong>',
'<em></em>',
'<u></u>',
'<a href=""></a>',
'<a href=""></a>',
'<div style="text-align: ;"></div>',
'<img src="" />',
'<a href="mailto:"></a>',
'<a href="mailto:"></a>',
'<span style="font-family:;"></span>',
'<span style="font-size:;"></span>',
'<span style="color:;"></span>',
);
// Replace Tags
$str = preg_replace ($pattern, $replace, $str);
// Replace Quotes
$open = '<blockquote>';
$close = '</blockquote>';
preg_match_all ('/\[quote\]/i', $str, $matches);
$opentags = count($matches['0']);
preg_match_all ('/\[\/quote\]/i', $str, $matches);
$closetags = count($matches['0']);
$unclosed = $opentags - $closetags;
for ($i = 0; $i < $unclosed; $i++)$str .= '</blockquote>';
$str = str_replace ('ใบเสนอราคาโดย หัวข้อ', $open, $str);
$str = str_replace ('[/' . 'quote]', $close, $str);
return $str;
}
?>
ตัวอย่าง :
<?php
$str = 'ตัวหนาตัวเอียงwww.goragod.com';
echo BBCodeFormat($str);
?>